<template>
	<div class="page">
		<!-- <div class="top">
			<div style="float:left;"><span @click="goBack" class="goback">触发器管理</span>　|　<span>添加新触发器</span><span id="notice" style="margin-left:0.5rem;">注:页面所有操作，保存之后才可生效</span></div>
			<el-button type="primary" size="small" id="goback" @click="goBack">返回</el-button>
		</div> -->
		<span id="notice" style="margin-left:0.5rem;">注:页面所有操作，保存之后才可生效</span>
		<el-form :model="form" :inline="true" id="addform" class="demo-form-inline">
			<div class="line">
			<el-form-item  label="项目名称:" :label-width="formLabelWidth2">
				<el-select v-model="form.product_id" placeholder="选择项目" class="inpu" @change="handleChange(form.product_id)">
					<el-option v-for="item in products" :key="item.value" :value="item.value" :label="item.label" :disabled="item.disabled">
					</el-option>
				</el-select>
				<!-- <el-cascader
					class="inpu"
					v-model="form.product_id"
					:options="products"
					:props="{checkStrictly:true,expandTrigger:'hover',emitPath:false}"
					@change="handleChange(form.product_id)">
				</el-cascader> -->
			</el-form-item>
			<el-form-item label="设备名称:" :label-width="formLabelWidth" >
				<el-select v-model="form.equip_id" placeholder="选择设备" class="inpu" @change="changeDevice(form.equip_id)">
					<el-option v-for="item in devices" :key="item.value" :value="item.value" :label="item.label">
					</el-option>
				</el-select>
				<el-tooltip placement="top" effect="light">
					<div slot="content" style="line-height:0.4rem;color:red;">如果是全部，则默认应用于整个项目，数据点地址需要手动输入;全部时，不支持联动和数据转发</div>
					<span class="sortIcon" style="color:red;">?</span>
				</el-tooltip>
				<!-- <div class="tip" style="width:auto;height:0.4rem;">如果是全部，则默认应用于整个项目，地址需要手动输入</div> -->
			</el-form-item>
			<span class="leftName">设备选择</span>
			</div>
			<div class="line">
			<el-form-item label="触发类型:" :label-width="formLabelWidth2" >
				<el-select v-model="form.trigger_type" placeholder="选择类型" class="inpu"  @change="changeTriggerType(form.trigger_type)">
					<el-option v-for="item in triggerTypes" :key="item.value" :value="item.value" :label="item.label"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="触发条件:" :label-width="formLabelWidth" v-if="form.trigger_type==2">
				<el-select v-model="form.condition" placeholder="选择条件" class="inpu"  @change="changeCondition(form.condition)">
					<el-option v-for="item in whileIfs2" :key="item.value" :value="item.value" :label="item.label"></el-option>
				</el-select>
				<el-tooltip v-if="form.condition==4" placement="top" effect="light">
					<div slot="content" style="line-height:0.4rem;color:red;">注：断电触发需要相关硬件的支持才可以</div>
					<span class="sortIcon" style="color:red;">!</span>
				</el-tooltip>
			</el-form-item>
			<el-form-item v-if="showType && form.trigger_type==1" label="子设备名称:" :label-width="formLabelWidth" >
				<el-select v-model="form.slave_id" placeholder="选择子设备" class="inpu"  @change="changeSlave(form.slave_id)">
					<el-option v-for="item in slaves" :key="item.value" :value="item.value" :label="item.label">
					</el-option>
				</el-select>
				<el-tooltip placement="top" effect="light">
					<div slot="content" style="line-height:0.4rem;color:red;">如果是全部，数据点地址需要手动输入</div>
					<span class="sortIcon" style="color:red;">?</span>
				</el-tooltip>
				<!-- <div class="tip" style="width:auto;height:0.4rem;">如果是全部，地址需要手动输入</div> -->
			</el-form-item>
			<el-form-item v-if="((!showType && form.equip_id==0) || (showType && form.slave_id==0)) && form.trigger_type==1" label="数据点地址:" :label-width="formLabelWidth">
				<el-input v-model="form.data_key" class="inpu" placeholder="请输入数据点地址"></el-input>
				<el-tooltip placement="top" effect="light">
					<div slot="content" style="line-height:0.4rem;color:red;">手动输入多个地址，请使用“,”(英文逗号)隔开，多个意为同时添加多个触发器</div>
					<span class="sortIcon" style="color:red;">?</span>
				</el-tooltip>
			</el-form-item>
			<el-form-item v-if="((!showType && form.equip_id!=0) || (showType && form.slave_id!=0)) && form.trigger_type==1" label="数据点地址:" :label-width="formLabelWidth" >
			    <el-select
			        v-model="form.data_key"
			        multiple
					collapse-tags
			        class="inpu big"
			    	style="float:right;height:0.95rem;margin-top:2px;"
			        placeholder="至少选择一个数据点">
			        <el-option
			    	  class="over"
			    	  style="text-align:left;padding-left:0.2rem;"
			          v-for="item in dataKeys"
			          :key="item.value"
			          :label="item.label"
			          :value="item.value">
			        </el-option>
			    </el-select>
			</el-form-item><br>
			<el-form-item label="触发条件:" :label-width="formLabelWidth2" v-if="form.trigger_type==1">
				<el-select v-model="form.condition" placeholder="选择条件" class="inpu" >
					<el-option v-for="item in whileIfs" :key="item.value" :value="item.value" :label="item.label"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item v-if="form.trigger_type==1 && form.condition!=6 && form.condition!=7" label="　"   label-width="64px" >
				值A：<el-input v-model="form.value1" class="inpu" style="width:1.85rem" placeholder="值A"></el-input>
				值B：<el-input :disabled="form.condition!=4 && form.condition!=5" v-model="form.value2" style="width:1.85rem;" class="inpu"
				 placeholder="值B"></el-input>
			</el-form-item>
			<el-form-item label="持续时间:" label-width="124px" v-if="form.trigger_type==1">
				<el-input v-model="form.keep_time" auto-complete="off" class="inpu" placeholder="请输入保持时间"></el-input> 秒
				<el-tooltip placement="top" effect="light" style="margin-left:0.1rem;margin-top:-2px;">
					<div slot="content" style="line-height:0.4rem;color:red;">只有持续触发报警超过该时间才会推送报警信息，否则不推送，0为立即推送</div>
					<span class="sortIcon" style="color:red;">?</span>
				</el-tooltip>
			    <!-- <div class="tip" style="width:auto;height:0.4rem;">只有持续触发报警超过该时间才会推送报警信息，否则不推送，0为立即推送</div> -->
			</el-form-item>
			<span class="leftName">条件设置</span>
			</div>
			<div class="line" style="padding-top:0.5rem;">
			<el-form-item label="设备报警:" :label-width="formLabelWidth2" style="margin-top:-0.2rem;">
				<el-switch v-model="form.sync_alarm" :width="stateWidth" class="demo" style="display:inline-block;vertical-align:top;"
				 active-color="#13ce66" inactive-color="#ccc" active-text="启用" inactive-text="禁用" :active-value="one"  :disabled="form.trigger_type==2 && form.condition==5" @change="changeAlarm(form.sync_alarm)"
			     :inactive-value="zero">
				</el-switch>
			</el-form-item>
		    <el-form-item label="报警推送:" :label-width="win.screen.width>1280?formLabelWidth:'80px'" style="margin-top:-0.2rem;margin-left:-1.6rem;">
				<el-switch v-model="form.is_send" :width="stateWidth" class="demo" style="display:inline-block;vertical-align:top;"
				 active-color="#13ce66" inactive-color="#ccc" active-text="启用" inactive-text="禁用" :active-value="one" :disabled="!form.sync_alarm" @change="changeSend(form.is_send)"
			     :inactive-value="zero">
				</el-switch>
			</el-form-item>
			<el-form-item label="恢复报警:" :label-width="win.screen.width>1280?formLabelWidth:'80px'" style="margin-top:-0.2rem;margin-left:-1.6rem;margin-bottom:10px;">
				<el-switch v-model="form.recover_alarm" :width="stateWidth" class="demo" style="display:inline-block;vertical-align:top;"
				 active-color="#13ce66" inactive-color="#ccc" active-text="启用" inactive-text="禁用" :active-value="one" :disabled="!form.is_send || !form.sync_alarm || form.trigger_type==2"
			     :inactive-value="zero">
				</el-switch>
			</el-form-item>
			<el-form-item label="" label-width="20" style="margin-top:-0.2rem;margin-left:-1rem;">
				<el-button  size="mini"  style="font-size:0.3rem;" @click="dialogAlarmVisible=true">报警说明</el-button>
			</el-form-item><br>
			<div v-if="form.is_send==1">
			<!-- <el-form-item label="微信报警:" :label-width="formLabelWidth2" style="margin-top:-0.2rem;">
				<span class="typeBox" :class="form.wx_alarm==1?'isChoose':'onChoose'"   @click="chooseAlarm('wx',form.wx_alarm)">√</span>
				<span style="margin-left:0.7rem;">需要有开启接收权限的平台账号，并绑定公众号，不产生费用</span>
			</el-form-item><br> -->
			<el-form-item label="企业微信报警:" :label-width="formLabelWidth2" style="margin-top:-0.2rem;">
				<span class="typeBox" :class="form.qy_alarm==1?'isChoose':'onChoose'"   @click="chooseAlarm('qy',form.qy_alarm)">√</span>
			</el-form-item>
			<el-form-item label="机器人URL:" :label-width="formLabelWidth" style="margin-top:-0.2rem;" >
				<el-input class="inpu"  style="width:10rem;margin-top:0.1rem;" v-model="form.qy_addr" placeholder="请输入企业微信的机器人url"  type="textarea" :rows="1" ></el-input>
			</el-form-item><br>
			<!-- <el-form-item label="短信报警:"  :label-width="formLabelWidth2" style="margin-top:-0.2rem;">
				<span class="typeBox" :class="form.sms_alarm==1?'isChoose':'onChoose'"  @click="chooseAlarm('sms',form.sms_alarm)">√</span>
			</el-form-item> -->
			<!-- <el-form-item label="联络人:" :label-width="formLabelWidth" style="margin-top:-0.2rem;" >
				<el-input class="inpu"  style="width:10rem;margin-top:0.1rem;" v-model="form.sms_contact" placeholder="请输入联络人的手机号,多个用英文逗号','间隔"  type="textarea" :rows="1" ></el-input>
			</el-form-item><br> -->
			<!-- <el-form-item label="语音报警:" :label-width="formLabelWidth2" style="margin-top:-0.15rem;">
				<span class="typeBox" :class="form.voice_alarm==1?'isChoose':'onChoose'"   @click="chooseAlarm('voice',form.voice_alarm)">√</span>
			</el-form-item>
			<el-form-item label="联络人:" :label-width="formLabelWidth"  style="margin-top:-0.15rem;">
				<el-input class="inpu"  style="width:10rem;margin-top:0.1rem;" v-model="form.voice_contact" placeholder="请输入联络人的手机号,多个用英文逗号','间隔"  type="textarea" :rows="1" ></el-input>
			</el-form-item><br> -->
			<el-form-item label="告警内容:" :label-width="formLabelWidth2" style="margin-top:-0.1rem;">
				<el-input class="inpu"  style="width:12.94rem;height:auto;" v-model="form.alarm_content" placeholder="有疑问可参考报警说明"  autosize   type="textarea" ></el-input>
			</el-form-item><br>
		    <div v-if="form.recover_alarm==1">
			<el-form-item  label="恢复告警内容:" :label-width="formLabelWidth2" style="margin-top:-0.16rem;">
				<el-input class="inpu"  style="width:12.94rem;" v-model="form.recover_content" placeholder="有疑问可参考报警说明" autosize  type="textarea" ></el-input>
			</el-form-item>
			</div>
			<!-- <el-form-item label="　　　　　" v-if="form.voice_alarm==1 || form.sms_alarm==1" :label-width="formLabelWidth2"> -->
				<!-- <p style="line-height: 0.4rem;">报警内容说明：报警提示的完整模板为："您好，您的设备(具体的设备名称)有新的告警，告警内容:(具体的告警内容)，告警时间:(具体的告警时间)。请尽快查看处理!"，配置时只需要将具体的告警内容填入输入框，其他内容不用填写，也不可改变！设备名称和告警时间会自动获取，无需配置！</p> -->
			<!-- </el-form-item> -->
			<el-form-item label="付款账号:" v-if="form.voice_alarm==1 || form.sms_alarm==1" :label-width="formLabelWidth2" style="margin-top:-0.1rem;">
				<el-select v-model="form.payer_id"  class="inpu">
					<el-option  class="oneOption" v-for="item in childrenFinance" :key="item.value" :value="item.value" :label="item.label"
						:style="{'text-align':'left','padding-left':'0.3rem'}">
					</el-option>
				</el-select>
			</el-form-item>
			</div>
			<span class="leftName">报警配置</span>
			</div>
			<div class="line" style="padding-top:0.5rem;padding-bottom:0.2rem;">
				    <el-form-item label="设备联动:" :label-width="win.screen.width>1280?formLabelWidth:'80px'" style="margin-bottom:2px;margin-left:1.2rem;">
				    	<el-switch v-model="form.is_linkage" :width="stateWidth" class="demo" style="display:inline-block;vertical-align:top;"
				    	 active-color="#13ce66" inactive-color="#ccc" active-text="启用" inactive-text="禁用" :active-value="one" :disabled="(form.trigger_type==2 && form.condition==5) || form.equip_id==0"
				         :inactive-value="zero">
				    	</el-switch>
				    </el-form-item>
					<el-form-item v-if="form.is_linkage==1 && form.trigger_type==2  && form.equip_id!=0" label="联动保留:" :label-width="win.screen.width>1280?formLabelWidth:'80px'" style="margin-bottom:2px;margin-left: -1.6rem;">
						<el-switch v-model="form.is_retain" :width="stateWidth" class="demo" style="display:inline-block;vertical-align:top;"
						 active-color="#13ce66" inactive-color="#ccc" active-text="启用" inactive-text="禁用" :active-value="one"
					     :inactive-value="zero">
						</el-switch>
					</el-form-item>
					<el-form-item   v-if="form.is_linkage==1"  label="　　　" :label-width="win.screen.width>1280?formLabelWidth:'80px'" style="margin-bottom:2px;margin-left: -2.6rem;">
			      	    <el-button size="mini" class="add" @click="addLink">添加</el-button>
					</el-form-item>
					<el-form-item label="　　　" :label-width="win.screen.width>1280?formLabelWidth:'80px'" style="margin-bottom:2px;margin-left:-1.8rem;">
						<el-button  size="mini"  style="font-size:0.3rem;" @click="dialogLinkageVisible=true">联动说明</el-button>
					</el-form-item><br>
			        <el-table
					      v-if="form.is_linkage==1"
			      	      :data="tableData"
			      		  size="mini"
			      		  :header-cell-style="{'fontSize':'0.3rem'}"
			      		  :cell-style="{'fontSize':'0.3rem'}"
			      		  style="width:90%;margin-left:80px;text-align:center;">
			      	      <el-table-column
			      	        prop="equip_id"
			      	        label="设备名称"
			      	        min-width="160">
			      	      </el-table-column>
			      	      <el-table-column
			      	        prop="slave_id"
			      	        label="子设备名称"
			      	        min-width="150">
			      	      </el-table-column>
			      	      <el-table-column
			      	        prop="sensor_name"
			      	        label="数据点地址"
			      			min-width="150">
			      	      </el-table-column>
			      		  <el-table-column
			      		    v-if="form.trigger_type==1"
			      		    prop="alarm_value"
			      		    label="触发下发值"
			      		  	min-width="100">
			      		  </el-table-column>
			      		  <el-table-column
			      		    v-if="form.trigger_type==1"
			      		    prop="recover_value"
			      		    label="恢复下发值"
			      		  	min-width="100">
			      		  </el-table-column>
			      		  <el-table-column
			      		    v-if="form.trigger_type==2 && (form.condition==1 || form.condition==2)"
			      		    prop="alarm_value"
			      		    label="上线下发值"
			      		  	min-width="100">
			      		  </el-table-column>
			      		  <el-table-column
			      		    v-if="form.trigger_type==2 && form.condition==3"
			      		    prop="alarm_value"
			      		    label="下线下发值"
			      		  	min-width="100">
			      		  </el-table-column>
						  <el-table-column
						    v-if="form.trigger_type==2 && form.condition==4"
						    prop="alarm_value"
						    label="断电下发值"
						  	min-width="100">
						  </el-table-column>
			      		  <el-table-column
			      		    v-if="form.trigger_type==2 && form.condition==1"
			      		    prop="recover_value"
			      		    label="下线下发值"
			      		  	min-width="100">
			      		  </el-table-column>
			      		  <el-table-column label="操作" min-width="120" align="center">
			      		  	<template slot-scope="scope">
			      				<el-button @click="linkEdit(scope.row,scope.$index)" type="text" style="color:#409Eff;font-size:0.3rem;" size="mini" >编辑</el-button>
			      		  		<el-button @click="sureDelete(scope.row)" type="text" size="mini" style="color:red;font-size:0.3rem" >删除</el-button>
			      		  	</template>
			                </el-table-column>
			          </el-table>
			          <span class="leftName">联动配置</span>
			    </div>
				<div class="line" style="padding-top:0.5rem;padding-bottom:0.2rem;">
					    <el-form-item label="数据转发:" :label-width="win.screen.width>1280?formLabelWidth:'80px'" style="margin-bottom:2px;margin-left:1.2rem;">
					    	<el-switch v-model="form.is_forward" :width="stateWidth" class="demo" style="display:inline-block;vertical-align:top;"
					    	 active-color="#13ce66" inactive-color="#ccc" active-text="启用" inactive-text="禁用" :active-value="one"  :disabled="(form.trigger_type==2 && form.condition!=5) || form.equip_id==0"
					         :inactive-value="zero">
					    	</el-switch>
					    </el-form-item>
						<el-form-item   v-if="form.is_forward==1"  label="　　　" :label-width="win.screen.width>1280?formLabelWidth:'80px'" style="margin-bottom:2px;margin-left: -2.6rem;">
						    <el-button size="mini" class="add" @click="addForward">添加</el-button>
						</el-form-item>
						<el-form-item label="　　　" :label-width="win.screen.width>1280?formLabelWidth:'80px'" style="margin-bottom:2px;margin-left:-1.8rem;">
							<el-button  size="mini"  style="font-size:0.3rem;" @click="forwardInfo">转发说明</el-button>
						</el-form-item><br>
						<el-table
						      v-if="form.is_forward==1"
						      :data="form.forwards"
							  size="mini"
							  :header-cell-style="{'fontSize':'0.3rem'}"
							  :cell-style="{'fontSize':'0.3rem'}"
							  style="width:90%;margin-left:80px;text-align:center;">
						      <el-table-column
						        prop="forward_equip_name"
						        label="设备名称"
						        min-width="160">
						      </el-table-column>
						      <el-table-column
						        prop=""
						        label="转发模板"
						        min-width="300">
								<template slot-scope="scope">
									<span>点击右侧编辑查看</span>
								</template>
							  </el-table-column>
						      <el-table-column label="操作" min-width="120" align="center">
							  	<template slot-scope="scope">
									<el-button @click="forwardEdit(scope.row,scope.$index)" type="text" style="color:#409Eff;font-size:0.3rem;" size="mini" >编辑</el-button>
							  		<el-button @click="sureDeleteForward(scope.row)" type="text" size="mini" style="color:red;font-size:0.3rem" >删除</el-button>
							  	</template>
						        </el-table-column>
						  </el-table>
						<span class="leftName">数据转发</span>
				</div>
		    <el-form-item style="margin-left:0.8rem;margin-top: 0.2rem;">
			    <el-button type="success" size="small" @click="addGoods">保存</el-button>
			    <el-button type="primary" size="small" @click="resetForm">重置</el-button>
			</el-form-item>
		</el-form>

<el-dialog :visible.sync="addFormVisible" width="30%" custom-class="dlog" append-to-body :show-close="close" top="150px">
    <div class="toptit">
        <span>添加联动</span>
        <span style="float:right;cursor: pointer;" @click="closeAddDialog">关闭</span>
    </div>
    <el-form :model="aboutForm" block style="margin-top:0.4rem;text-align: left;">
        <el-form-item label="设备名称:" :label-width="formLabelWidth">
            <el-select v-model="aboutForm.linkage_equip_id" placeholder="选择设备" class="inpu" @change="changeOnDevice(aboutForm.linkage_equip_id)">
                <el-option v-for="item in onDevices" :key="item.value" :value="item.value" :label="item.label">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="子设备名称:" :label-width="formLabelWidth" v-if="aboutForm.type==1">
            <el-select v-model="aboutForm.linkage_slave_id" placeholder="选择子设备" class="inpu" @change="changeOnSlave(aboutForm.linkage_slave_id)">
                <el-option v-for="item in onSlaves" :key="item.value" :value="item.value" :label="item.label">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="数据点地址:" :label-width="formLabelWidth">
            <el-select v-model="aboutForm.linkage_sensor_id" placeholder="选择数据点,单选" class="inpu">
                <el-option v-for="item in onSensors" :key="item.value" :value="item.value" :label="item.label">
                </el-option>
            </el-select>
        </el-form-item>

        <el-form-item v-if="form.trigger_type==1" label="触发下发值:" :label-width="formLabelWidth">
            <el-input v-model="aboutForm.linkage_value" class="inpu" style="width:2.25rem;"></el-input>
        </el-form-item>
        <el-form-item v-if="form.trigger_type==1" label="恢复下发值:" :label-width="formLabelWidth">
            <el-input v-model="aboutForm.linkage_recover_value" class="inpu" style="width:2.25rem;"></el-input>
            <span style="color:red;font-size:0.3rem;margin-left:0.1rem;"> 为空则不下发</span>
		</el-form-item>
		<el-form-item v-if="form.trigger_type==2 && (form.condition==1 || form.condition==2)" label="上线下发值:" :label-width="formLabelWidth">
		    <el-input v-model="aboutForm.linkage_value" class="inpu" style="width:2.25rem;"></el-input>
		</el-form-item>
		<el-form-item v-if="form.trigger_type==2 && form.condition==3" label="下线下发值:" :label-width="formLabelWidth">
		    <el-input v-model="aboutForm.linkage_value" class="inpu" style="width:2.25rem;"></el-input>
		</el-form-item>
		<el-form-item v-if="form.trigger_type==2 && form.condition==1" label="下线下发值:" :label-width="formLabelWidth">
		    <el-input v-model="aboutForm.linkage_recover_value" class="inpu" style="width:2.25rem;"></el-input>
		</el-form-item>
		<el-form-item v-if="form.trigger_type==2 && form.condition==4" label="断电下发值:" :label-width="formLabelWidth">
		    <el-input v-model="aboutForm.linkage_value" class="inpu" style="width:2.25rem;"></el-input>
		</el-form-item>
    </el-form>
    <div style="text-align:right;">
        <el-button size="small" @click="closeAddDialog">取 消</el-button>
        <el-button type="primary" size="small" @click="sureLink">确 定</el-button>
    </div>
</el-dialog>
<el-dialog :visible.sync="editFormVisible" width="30%" custom-class="dlog" append-to-body :show-close="close" top="150px">
    <div class="toptit">
        <span>修改联动</span>
        <span style="float:right;cursor: pointer;" @click="cancelEdit">关闭</span>
    </div>
    <el-form :model="editForm" block style="margin-top:0.4rem;text-align: left;">
        <el-form-item label="设备名称:" :label-width="formLabelWidth">
            <el-select v-model="editForm.linkage_equip_id" placeholder="选择设备" class="inpu" @change="changeEditDevice(editForm.linkage_equip_id)">
                <el-option v-for="item in onDevices" :key="item.value" :value="item.value" :label="item.label">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="子设备名称:" :label-width="formLabelWidth" v-if="editForm.type==1">
            <el-select v-model="editForm.linkage_slave_id" placeholder="选择子设备" class="inpu" @change="changeEditSlave(editForm.linkage_slave_id)">
                <el-option v-for="item in linkSlaves" :key="item.value" :value="item.value" :label="item.label">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="数据点地址:" :label-width="formLabelWidth">
            <el-select v-model="editForm.linkage_sensor_id" placeholder="选择数据点,单选" class="inpu">
                <el-option v-for="item in linkSensors" :key="item.value" :value="item.value" :label="item.label">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item v-if="form.trigger_type==1" label="触发下发值:" :label-width="formLabelWidth">
		    <el-input v-model="editForm.linkage_value" class="inpu" style="width:2.25rem;"></el-input>
		</el-form-item>
		<el-form-item v-if="form.trigger_type==1" label="恢复下发值:" :label-width="formLabelWidth">
		    <el-input v-model="editForm.linkage_recover_value" class="inpu" style="width:2.25rem;"></el-input>
		    <span style="color:red;font-size:0.3rem;margin-left:0.1rem;"> 为空则不下发</span>
		</el-form-item>
		<el-form-item v-if="form.trigger_type==2 && (form.condition==1 || form.condition==2)" label="上线下发值:" :label-width="formLabelWidth">
		    <el-input v-model="editForm.linkage_value" class="inpu" style="width:2.25rem;"></el-input>
		</el-form-item>
		<el-form-item v-if="form.trigger_type==2 && form.condition==3" label="下线下发值:" :label-width="formLabelWidth">
		    <el-input v-model="editForm.linkage_value" class="inpu" style="width:2.25rem;"></el-input>
		</el-form-item>
		<el-form-item v-if="form.trigger_type==2 && form.condition==1" label="下线下发值:" :label-width="formLabelWidth">
		    <el-input v-model="editForm.linkage_recover_value" class="inpu" style="width:2.25rem;"></el-input>
		</el-form-item>
		<el-form-item v-if="form.trigger_type==2 && form.condition==4" label="断电下发值:" :label-width="formLabelWidth">
		    <el-input v-model="editForm.linkage_value" class="inpu" style="width:2.25rem;"></el-input>
		</el-form-item>
    </el-form>
    <div style="text-align:right;">
        <el-button size="small" @click="cancelEdit">取 消</el-button>
        <el-button type="primary" size="small" @click="sureEdit">确 定</el-button>
    </div>
</el-dialog>
<el-dialog :visible="addForwardVisible" width="55%" v-el-dialog-drag v-el-dialog-drag-width :close-on-click-modal="false" class="datadial" :append-to-body="true" :show-close="close" top="150px">
    <div class="toptit">
        <span>添加转发</span>
        <span style="float:right;cursor: pointer;" @click="cancelAddForward">关闭</span>
    </div>
    <el-form :model="addForwardForm" inline label-width="auto" style="margin-top:0.4rem;text-align: left;">
        <el-form-item label="接收设备:">
            <el-select v-model="addForwardForm.forward_equip_id" placeholder="选择设备" class="inpu"  @change="changeForwardEquip(addForwardForm.forward_equip_id,'add')">
                <el-option v-for="item in forwardDevices" :key="item.value" :value="item.value" :label="item.label">
                </el-option>
            </el-select>
        </el-form-item>
		<el-form-item>
			<el-button size="small" @click="addDomain('add')">添加转发点</el-button>
			<!-- <el-button size="small" v-if="!drawerShow" @click="varStringHelp">打开公式生成器</el-button> -->
			<!-- <el-button size="small" v-else  @click="varStringHelp">关闭公式生成器</el-button> -->
			<el-button size="small" @click="formulaInfoVisible=true" >计算公式说明</el-button>
		</el-form-item>
		<div class="sensorHead" ref="sensorHead">
			<span class="inpu" v-if="addForwardForm.equip_type==1">接收子设备</span>
			<span class="inpu" >接收数据点</span>
			<span class="inpu" style="width:7.5rem;">源数据点及计算公式</span>
			<span class="inpu" style="width:1rem" v-if="addForwardForm.forward_template.length>1">操作</span>
		</div>
		<el-form-item v-for="(domain, index) in addForwardForm.forward_template" class="oneDomain" :prop="'forward_template.' + index + '.sensor_id'"
		 :key="index">
			<el-select v-if="addForwardForm.equip_type==1" v-model="domain.slave_id" placeholder="子设备" class="inpu" @change="changeForwardSlave(domain.slave_id,index,'add')">
				<el-option v-for="item in forwardSlaves" :key="item.value" :value="item.value" :label="item.label">
				</el-option>
			</el-select>
			<el-select v-model="domain.data_key" placeholder="数据点" class="inpu" >
				<el-option v-for="item in domain.sensors" :key="item.value" :value="item.value" :label="item.label">
				</el-option>
			</el-select>
			<!-- 由于组件不支持点击事件，所以加后缀native -->
			<el-input class="inpu over" style="width:7.5rem;" v-model="domain.formula" type="text" :title="domain.formula"   placeholder="双击自动填入(或替换)右侧计算公式" @dblclick.native="fillString(domain)"></el-input>
			<!-- <el-input class="inpu" style="width:6rem;" v-model="domain.formula" type="textarea"  :autosize="{ minRows: 1, maxRows: 4 }" placeholder="计算公式"></el-input> -->
			<el-button @click.prevent="removeDomain(domain,'add')" type="danger" size="mini" v-if="addForwardForm.forward_template.length>1">删除</el-button>
		</el-form-item>
        <!-- <el-form-item label="转发模板:" :label-width="formLabelWidth">
			<el-input v-model="addForwardForm.forward_template" class="inpu"  style="width:10rem;" type="textarea"  :rows="6"></el-input>
        </el-form-item> -->
    </el-form>
    <div style="text-align:right;margin-top:0.3rem;">
        <el-button size="small" @click="cancelAddForward">取 消</el-button>
        <el-button type="primary" size="small" @click="sureAddForward">确 定</el-button>
    </div>
	<FormulaCreater
	   :drawerShow="drawerShow"
	   :sensorTree="sensorTree"
	   :getSensorString="getSensorString"
	></FormulaCreater>
</el-dialog>

<el-dialog :visible="editForwardVisible" width="55%" v-el-dialog-drag v-el-dialog-drag-width class="datadial" append-to-body :close-on-click-modal="false" :show-close="close" top="150px">
    <div class="toptit">
        <span>编辑转发</span>
        <span style="float:right;cursor: pointer;" @click="cancelEditForward">关闭</span>
    </div>
    <el-form :model="editForwardForm" inline label-width="auto"  style="margin-top:0.4rem;text-align: left;">
        <el-form-item label="接收设备:">
            <el-select v-model="editForwardForm.forward_equip_id" placeholder="选择设备" class="inpu" @change="changeForwardEquip(editForwardForm.forward_equip_id,'edit')">
                <el-option v-for="item in forwardDevices" :key="item.value" :value="item.value" :label="item.label">
                </el-option>
            </el-select>
        </el-form-item>
		<el-form-item >
			<el-button size="small" @click="addDomain('edit')">添加转发点</el-button>
			<!-- <el-button size="small" v-if="!drawerShow" @click="varStringHelp">打开公式生成器</el-button> -->
			<!-- <el-button size="small" v-else  @click="varStringHelp">关闭公式生成器</el-button> -->
		    <el-button size="small" @click="formulaInfoVisible=true" >计算公式说明</el-button>
		</el-form-item>
		<div class="sensorHead" ref="sensorHead">
			<span class="inpu" v-if="editForwardForm.equip_type==1">接收子设备</span>
			<span class="inpu" >接收数据点</span>
			<span class="inpu" style="width:7.5rem;">源数据点及计算公式</span>
			<span class="inpu" style="width:1rem" v-if="editForwardForm.forward_template.length>1">操作</span>
		</div>
		<el-form-item v-for="(domain, index) in editForwardForm.forward_template" class="oneDomain" :prop="'forward_template.' + index + '.sensor_id'"
		 :key="index">
			<el-select v-if="editForwardForm.equip_type==1" v-model="domain.slave_id" placeholder="子设备" class="inpu" @change="changeForwardSlave(domain.slave_id,index,'edit')">
				<el-option v-for="item in forwardSlaves" :key="item.value" :value="item.value" :label="item.label">
				</el-option>
			</el-select>
			<el-select v-model="domain.data_key" placeholder="数据点" class="inpu" >
				<el-option v-for="item in domain.sensors" :key="item.value" :value="item.value" :label="item.label">
				</el-option>
			</el-select>
			<!-- 由于组件不支持点击事件，所以加后缀native -->
			<el-input class="inpu over" style="width:7.5rem;" v-model="domain.formula" type="text" :title="domain.formula"   placeholder="双击自动填入(或替换)右侧计算公式" @dblclick.native="fillString(domain)"></el-input>
			<!-- <el-input class="inpu" style="width:6rem;" v-model="domain.formula" type="textarea"  :autosize="{ minRows: 1, maxRows: 4 }" placeholder="计算公式"></el-input> -->
			<el-button @click.prevent="removeDomain(domain,'edit')" type="danger" size="mini" v-if="editForwardForm.forward_template.length>1">删除</el-button>
		</el-form-item>
        <!-- <el-form-item label="转发模板:" :label-width="formLabelWidth" >
			<el-input v-model="editForwardForm.forward_template" class="inpu" style="width:10rem;" type="textarea"  :rows="6"></el-input>
        </el-form-item> -->
    </el-form>
    <div style="text-align:right;margin-top:0.3rem;">
        <el-button size="small" @click="cancelEditForward">取 消</el-button>
        <el-button type="primary" size="small" @click="sureEditForward">确 定</el-button>
    </div>
	<FormulaCreater
	   :drawerShow="drawerShow"
	   :sensorTree="sensorTree"
	   :getSensorString="getSensorString"
	></FormulaCreater>
</el-dialog>

<el-dialog :visible="formulaInfoVisible"  :width="win.screen.width>=1600?'44%':'64%'"  :show-close="false" append-to-body top="200px" class="dial">
	<div class="toptit">
		<span>计算公式说明</span>
		<span style="float:right;cursor: pointer;" @click="formulaInfoVisible=false">关闭</span>
	</div>
	<FormulaInfo></FormulaInfo>
</el-dialog>
<el-dialog :visible="dialogAlarmVisible"  :width="win.screen.width>=1600?'42%':'62%'"  :show-close="false" append-to-body top="200px" class="dial">
	<div class="toptit">
		<span>报警说明</span>
		<span style="float:right;cursor: pointer;" @click="dialogAlarmVisible=false">关闭</span>
	</div>
	<div class="infocontent" style="margin-top:0.2rem;">
		<p>
		 <h4>开关说明：</h4>
		 设备报警：当前触发器是否作为报警触发器使用；<br>
		 报警推送：当数据点达到条件时,是否发送报警消息,只有作为报警触发时,此项才可使用；<br>
		 恢复报警：当数据点恢复正常时,是否发送提示消息,只有开启报警推送后,此项才可使用；
		 <h4>告警内容说明：</h4>
		  1、告警通知模板：尊敬的云控物联平台用户，您的设备“<设备名称>”有新的告警，告警内容：<告警内容>，请及时查看处理!(微信/短信/语音格式略有不同);<br>
		  2、告警通知仅支持告警内容自定义，其他为固定格式不支持编辑;<br>
		  3、告警内容支持的变量有： 分组名称：{groupName}，数据点名称：{sensorName}，报警条件：{condition}，报警值：{value};<br>
		  4、告警内容为空则默认系统格式，系统格式：{groupName} {sensorName} {condition};<br>
		  5、短信告警内容实际长度为1~35字符，超过将无法发送，不支持表情字符等特殊字符;
	  <!-- <h4>报警计费规则：</h4>
		 1、短信通知每条0.05元，长度70个字符以内按单条计费，长度超过70个字符按65个字符拆分成多条计费;<br>
		 2、语音通知每分钟（约65个字符播放三次）0.1元，1分钟以内按1分钟计费;
		 -->
		</p>
	</div>
</el-dialog>
<el-dialog :visible="dialogLinkageVisible"  :width="win.screen.width>=1600?'42%':'62%'"  :show-close="false" append-to-body top="200px" class="dial">
	<div class="toptit">
		<span>联动说明</span>
		<span style="float:right;cursor: pointer;" @click="dialogLinkageVisible=false">关闭</span>
	</div>
	<div class="infocontent" style="margin-top:0.2rem;">
		<p>
		 <h4>开关说明：</h4>
		 设备联动：当数据点达到条件时,是否触发其他(含其他设备)的数据点发生改变(写值),可与报警结合使用；<br>
		 联动保留：触发类型为上下线触发,当被联动设备重新上线时,是否触发联动(只触发联动设备当前状态的对应事件)；
		 <h4>注意事项：</h4>
		 1.设备报警和设备联动及数据转发请至少启用一项,否则将失去意义；<br>
		 2.添加或编辑联动时所绑定的数据点必须是读写变量，会自动屏蔽只读变量；<br>
		 3.当触发器只作为设备联动触发时,请关闭设备报警项,否则可能会不停发送报警信息(或提示)；<br>
		 4.当被联动设备的版本高于5.1时,联动保留才会生效；
		</p>
	</div>
</el-dialog>
<el-dialog :visible="forwardInfoVisible"  :width="win.screen.width>=1600?'42%':'62%'"  :show-close="false" append-to-body top="200px" class="dial">
	<div class="toptit">
		<span>转发说明</span>
		<span style="float:right;cursor: pointer;" @click="forwardInfoVisible=false">关闭</span>
	</div>
	<div class="infocontent" style="margin-top:0.2rem;">
		 <!-- <h2 style="width:100%;text-align: center;margin-top:0.2rem;"></h2><br> -->
		<p>
		 1.支持设备内部多个数据点进行简单四则运算，暂不提供函数计算；<br>
		 2.转发只支持单设备的数据触发和数据上报事件触发；
	    </p>
	</div>
</el-dialog>
<!-- <el-dialog :visible.sync="forwardTemplateVisible" width="66%" custom-class="dlog" append-to-body :show-close="close" top="150px">
    <div class="toptit">
        <span>转发模板详情 | {{forwardTemplateInfo.forward_equip_name}}</span>
        <span style="float:right;cursor: pointer;" @click="forwardTemplateVisible=false">关闭</span>
    </div>
    <div class="sensorHead" ref="sensorHead">
    	<span class="inpu" v-if="addForwardForm.equip_type==1">子设备</span>
    	<span class="inpu" >数据点</span>
    	<span class="inpu" style="width:10rem;">计算公式</span>
    </div>
	<div>

	</div>

</el-dialog> -->

</div>
</template>

<script>
    import {
        whileIfs
    } from '@/common.js';
	import FormulaCreater from '../templates/FormulaCreater.vue';
	import FormulaInfo from '../templates/FormulaInfo.vue';
    export default {
        name: 'addHappen',
        data() {
            return {
				win:window,
               // grade: localStorage.getItem("grade"),
                one: 1,
                zero: 0,
                user: [],
				formLabelWidth2: '160px',
                formLabelWidth: '100px',
                value: '',
                devid: null,
                close: false,
                showType: false,
                dataKeys: [],
                onSensors: [],
                tableData: [],
				//forwardData:[],
                linkData: [],
                linkSlaves: [],
                linkSensors: [],
                stateWidth: 60,
                editIndex: 0,
				editForwardIndex:0,
                baseurl: this.$baseurl,
				products: [],
				product_id:'',
				hasDefault:false,
                showTable: false,
                addFormVisible: false,
                editFormVisible: false,
				dialogAlarmVisible:false,
				dialogLinkageVisible:false,
				dialogForwardVisible:false,
				addForwardVisible:false,
				editForwardVisible:false,
				forwardInfoVisible:false,
				forwardTemplateVisible:false,
				formulaInfoVisible:false,
				drawerShow:false,
				forwardDevices:[],
				sibling:[],
				isCopy:false,
				sensorString:'',
                showdevice: false,
                triggerTypes: [{
                    value: 1,
                    label: '数据触发'
                }, {
                    value: 2,
                    label: '事件触发'
                }],
                editForm: {
					"type":1,
                    "linkage_equip_id": '',
                    "linkage_slave_id": '',
                    "linkage_sensor_id": '',
                    "linkage_value": '',
                    "linkage_recover_value": '',
                },
                aboutForm: {
					"type":1,
                    "linkage_equip_id": '',
                    "linkage_slave_id": '',
                    "linkage_sensor_id": '',
                    "linkage_value": 1,
                    "linkage_recover_value": 0,
                },
				addForwardForm:{
					forward_equip_id:'',
					forward_equip_code:'',
					forward_equip_name:'',   //追加的字段，用以在表格中显示名称
					equip_type:1,
					forward_template:[
						{
							slave_id:0,
							sensors:[],
						    slave_no: "",
							//sensor_id:0,
							data_key: "",
							formula:""
						}
					]
				},
				editForwardForm:{
					forward_equip_id:'',
					forward_equip_code:'',
					forward_equip_name:'',  //追加的字段，用以在表格中显示名称
					equip_type:1,
					forward_template:[{

					}]
				},
				forwardSlaves:[{
					value:0,
					label:''
				}],
				equipName:'',
				filterText:'',
				sensorTree:[],
				sensorProps:{
					id:'',
					label:'',
					children:[]
				},
                form: {
                    product_id:'',
                    equip_id:'',
                    slave_id: 0,
                    data_key: '',
                    keep_time: 0,
                    trigger_type: 1,
                    condition: 1,
                    value1:1,
                    value2: '',
                    recover_alarm: 0,
					sync_alarm:1,
					wx_alarm:1,
					qy_alarm:0,
					sms_alarm:0,
					voice_alarm:0,
					//email_alarm:0,
					qy_addr:'',
					sms_contact:'',
					voice_contact:'',
					recover_content:'',
					alarm_content:'',
					payer_id:0,
                    // send_wx: 0,
                    // send_sms: 0,
                    is_send: 1,
                    is_linkage: 0,
					is_retain:0,
					linkages:'',
					is_forward:0,
					forwards:[]
                },
                devices: [],
                slaves: [{
                    value: 0,
                    label: "全部"
                }],
                onDevices: [],
                onSlaves: [],
                whileIfs2: [{
                    value: 1,
                    label: '上下线'
                }, {
                    value: 2,
                    label: '上线'
                }, {
                    value: 3,
                    label: '下线'
                },{
                    value: 4,
                    label: '断电'
                },
				{
				    value: 5,
				    label: '数据上报'
				}
				],
                whileIfs: whileIfs,
                setForm: {
                    name: '无'
                },
				children:[],
				childrenFinance:[]
            }
        },

		components:{
			FormulaCreater,
			FormulaInfo
		},

		// watch: {
		//    filterText(val) {
		// 	  this.$refs.tree.filter(val);
		//    }
		// },

        created() {
            this.getProduct();
           // this.getChildren();
        },
        methods: {
			getProduct(){
				var that=this;
				this.$http({
					method: 'post',
					url: '/api/product/menuTree',
					data: {}
				}).then(res => {
					if (res && res.data.result == "success") {
						this.products=res.data.data;
						this.getFirstDefault(this.products[0]);
					}

				}).catch(error => {

					console.log(error);
				});
			},


			getFirstDefault(item){
				if(item.equips_count && !this.hasDefault){
					this.form.product_id = item.value;
					this.hasDefault = true;
					this.getDevice(this.form.product_id);
					return true;
				}else if(!this.hasDefault && item.children && item.children.length){
					for(var i=0,len=item.children.length;i<len;i++){
						this.getFirstDefault(item.children[i]);
					}
				}
			},

			handleChange(par){
				this.onDevices = [];
				this.devices = [];
				var that = this;
        this.form.equip_id = '';
        this.form.slave_id = '';
				this.form.data_key = '';
				this.aboutForm.linkage_slave_id = '';
				this.aboutForm.linkage_sensor_id = '';
				this.getDevice(par);
				// setTimeout(function(){
				// 	that.changeOnDevice(that.onDevices[0].value);
				// },1000);
			},

            resetForm() {
                for (var key in this.form) {
                    this.form[key] = "";
                }
                this.form.is_send = 1;
				this.form.sync_alarm = 1;
                this.form.recover_alarm = 0;
                this.form.is_linkage = 0;
				this.form.is_retain = 0;
                this.form.condition = 1;
				this.form.value1=1;
				this.form.product_id = 1;
                this.form.equip_id = '';
                this.form.slave_id = this.slaves[0].value;
                this.form.trigger_type = 1;
                this.form.keep_time = 0;
				this.form.wx_alarm = 1;
				this.form.qy_alarm = 0;
				this.form.sms_alarm = 0;
				this.form.voice_alarm = 0;
				this.form.is_forward = 0;
				this.form.payer_id = this.childrenFinance.length?this.childrenFinance[0].value:'';
                this.aboutForm.data_key = '';
                this.aboutForm.alarm_value = '';
                this.aboutForm.recover_value = '';
				this.tableData = [];
				this.form.forwards = [];
                this.linkData = [];
            },
            goBack() {
                this.$router.go(-1);
            },

			chooseAlarm(type,value){
				if(type=="wx"){
					this.form.wx_alarm=value==1?0:1;
				}else if(type=="sms"){
					this.form.sms_alarm=value==1?0:1;
				}else if(type=="voice"){
					this.form.voice_alarm=value==1?0:1;
				}else if(type=="qy"){
					this.form.qy_alarm=value==1?0:1;
				}
			},

			getChildren(id){
				this.children=[];
				this.childrenFinance=[];
				this.$http({
					method: 'post',
					url: 'api/user/contact',
					data:{
						'id':localStorage.getItem("at_userId"),
					}
				}).then(res => {
					if (res && res.data.result == "success") {
						for(let i=0,len=res.data.data.length;i<len;i++){
							// let child={
							// 	value:Number(res.data.data[i].id),
							// 	label:res.data.data[i].user_name
							// }
							// this.children.push(child);
							if(res.data.data[i].allow_finance==1){
								//开通了付费的用户
								let child={
									value:Number(res.data.data[i].id),
									label:res.data.data[i].user_name
								}
								this.childrenFinance.push(child);
							}
						}
						this.form.payer_id = this.childrenFinance.length?this.childrenFinance[0].value:'';
					}
				}).catch(error => {
					console.log(error);

				});
			},


            //获取项目下所有设备函数
            getDevice(id) {
                var that = this;
                this.onDevices = [];
                this.devices = [];
                this.$http({
                    method: 'post',
                    url: 'api/equip/all',
                    data: {
                        'product_id': id
                    }
                }).then(res => {
                    if (res && res.data.result == "success") {
                        if (res.data.data.length) {
                            for (var i = 0, len = res.data.data.length; i < len; i++) {
                                var device = {
                                    value: res.data.data[i].id,
                                    label: res.data.data[i].equip_name,
                                    type: res.data.data[i].type,
									equipCode:res.data.data[i].equip_code,
                                    slaveCount: res.data.data[i].slaves_count
                                }
                                this.devices.push(device);
                                //this.onDevices.push(device);
                            }
							this.onDevices=Array.from(that.devices);
							this.form.equip_id = this.devices[0].value;
                            if (this.devices[0].type == 1 && this.devices[0].slaveCount > 1) {
                                this.showType = true;
								this.getSlave(this.form.equip_id);
                            } else {
                                this.showType = false;
								this.form.slave_id = '';
								this.getSensor(this.form.equip_id);
                            }
                            this.aboutForm.linkage_equip_id = this.onDevices[0].value;
                            this.aboutForm.type = this.onDevices[0].type;
                            if (this.aboutForm.type == 1) {
                                this.getOnSlave(this.onDevices[0].value);
                            } else {
								this.getOnSensor(this.onDevices[0].value);
							}
                        }
                    }
                }).catch(error => {
                    console.log(error);
                });
            },

            getSlave(id) {
                var that = this;
                this.slaves = [{
                    value: 0,
                    label: "全部",
                }];
                this.$http({
                    method: 'post',
                    url: 'api/slave/all',
                    data: {
                        "equip_id": id

                    }
                }).then(res => {
                    if (res && res.data.result == "success") {
                        for (var i = 0; i < res.data.data.length; i++) {
                            var slave = {
                                value: res.data.data[i].id,
								slave_no:res.data.data.slave_no,
                                label: res.data.data[i].slave_name
                            }
                            that.slaves.push(slave)
                        }
                        that.form.slave_id = that.slaves[0].value;
                    }
                    // console.log(that.pointdata[0].icon)
                }).catch(error => {
                    console.log(error);

                });
            },
            async getOnSlave(id) {
                var that = this;
                this.onSlaves = [];
                await this.$http({
                    method: 'post',
                    url: 'api/slave/all',
                    data: {
                        "equip_id": id

                    }
                }).then(async res => {
                    if (res && res.data.result == "success") {
                        for (let i = 0; i < res.data.data.length; i++) {
                            let onslave = {
                                value: res.data.data[i].id,
                                label: res.data.data[i].slave_name
                            }
                            this.onSlaves.push(onslave)
                        }
						this.aboutForm.linkage_slave_id = this.onSlaves[0].value;
                        await this.getOnSensor(that.onSlaves[0].value);
						if(that.onSensors.length){
							that.aboutForm.linkage_sensor_id = that.onSensors[0].value;
						}
						return true;
                    }
                    // console.log(that.pointdata[0].icon)
                }).catch(error => {
                    console.log(error);

                });
            },

            getSensor(id) {
                var that = this;
                this.dataKeys = [];
                this.$http({
                    method: 'post',
                    url: 'api/sensor/all',
                    data: this.showType ? {
                        "slave_id": id

                    } : {
                        "equip_id": id

                    }
                }).then(res => {
                    if (res && res.data.result == "success") {
                        for (let i = 0, len = res.data.data.length; i < len; i++) {
                            let dataKey = {
                                value: res.data.data[i].data_key,
                                label: res.data.data[i].data_key + ' (' + res.data.data[i].sensor_name + ')'
                            }
                            that.dataKeys.push(dataKey)
                        }
                    }

                }).catch(error => {
                    console.log(error)
                });

            },

            async getOnSensor(id) {
                var that = this;
                this.onSensors = [];
                await this.$http({
                    method: 'post',
                    url: 'api/sensor/all',
                    data: this.aboutForm.type == 1? {
                        "slave_id": id

                    } : {
                        "equip_id": id

                    }
                }).then(res => {
                    if (res && res.data.result == "success") {
                        for (let i = 0, len = res.data.data.length; i < len; i++) {
                            if (res.data.data[i].read_write == 2) {
                                let onSensor = {
                                    value: res.data.data[i].id,
                                    label: res.data.data[i].data_key + ' (' + res.data.data[i].sensor_name + ')'
                                }
                                that.onSensors.push(onSensor);
                            }
                        }
						return true;
                    }else{
						return false;
					}

                }).catch(error => {
                    console.log(error);
					return false;
                });

            },

            changeTriggerType(type){
				if(type==2){
					this.form.recover_alarm = 0;
				}
			},

            changeSend(send) {
                if (!send) {
                    this.form.recover_alarm = 0;
                }
            },

			changeAlarm(isAlarm){
				if (!isAlarm) {
					this.form.is_send = 0;
				    this.form.recover_alarm = 0;
				}
			},

            changeDevice(device) {
                this.form.data_key = '';
                if (Number(device)) {
                    for (let i = 0; i < this.devices.length; i++) {
                        if (device == this.devices[i].value) {
							this.equipName=this.devices[i].label;
                            if ((this.devices[i].type == 1) && (this.devices[i].slaveCount > 1)) {
                                this.showType = true;
                                this.getSlave(device);
                            } else {
                                this.showType = false;
                                this.form.slave_id = '';
                                this.getSensor(device);
                            }
                            break;
                        }
                    }
                }else{
					this.form.is_forward=0;
					this.form.is_linkage=0;
				}
            },

			changeSlave(slave) {
                this.form.data_key = '';
                if (slave) {
                    this.getSensor(slave)
                }
            },

            async changeOnSlave(slave) {
                var that = this;
                if (slave) {
                    await this.getOnSensor(slave);
                    setTimeout(function() {
						if(that.onSensors.length){
                           that.aboutForm.linkage_sensor_id = that.onSensors[0].value;
						}
                    },500);
                }
            },

            // changePro(name) {
            //     var that = this;
            //     this.form.data_key = '';
            //     this.aboutForm.linkage_slave_id = '';
            //     this.aboutForm.linkage_sensor_id = '';
            //     this.getDevice(name);
            //     // setTimeout(function(){
            //     // 	that.changeOnDevice(that.onDevices[0].value);
            //     // },1000);
            // },
            async changeOnDevice(device) {
                var that = this;
				this.onSlaves=[];
				this.onSensors=[];
				this.aboutForm.linkage_slave_id = '';
				this.aboutForm.linkage_sensor_id = '';
                for (let i = 0; i < this.onDevices.length; i++) {
                    if (this.onDevices[i].value == device) {
                        this.aboutForm.type = this.onDevices[i].type;
                        break;
                    }
                }
                if (this.aboutForm.type == 1) {
                    await this.getOnSlave(device);
                    that.aboutForm.linkage_slave_id = that.onSlaves[0].value;
                } else if(this.aboutForm.type == 2){
                    this.aboutForm.linkage_slave_id = '';
                    await this.getOnSensor(device);
					if(that.onSensors.length){
					   that.aboutForm.linkage_sensor_id = that.onSensors[0].value;
					}
                }
            },
            // changeOnPro(name) {
            // 	var that=this;
            // 	this.aboutForm.slave_id='';
            // 	this.aboutForm.sensor_name='';
            //     this.getOnDevice(name);
            // 	setTimeout(function(){
            // 		that.changeOnDevice(that.onDevices[0].value);
            // 	},1000);
            // },


            addLink() {
                this.addFormVisible = true;
                this.aboutForm.linkage_equip_id = this.onDevices[0].value;
                this.changeOnDevice(this.aboutForm.linkage_equip_id);
            },

            sureLink() {
                var that = this;
                //this.showTable = true;
                if (!this.aboutForm.linkage_value && (!this.aboutForm.linkage_recover_value)) {
                    this.$message({
                        message: '触发下发值和恢复下发值不能同时为空!',
                        type: 'error',
                        center: true
                    });
                    return false;
                }
                //console.log("a");
                //引用型数据不能等号传值，会相互影响，要拷贝；
                function copy(obj) {
                    return Object.assign({}, obj);
                    //return {...obj};//也可以用扩展运算符
                };
                var about = copy(this.aboutForm);
                delete about.type;
                this.linkData.push(about);
                var change = {
                    "equip_id": '',
                    "slave_id": '',
                    "sensor_name": '',
                    "alarm_value": this.aboutForm.linkage_value,
                    "recover_value": this.aboutForm.linkage_recover_value,
                    "type": this.aboutForm.type
                };
                for (let i = 0, len = this.onDevices.length; i < len; i++) {
                    if (this.onDevices[i].value == this.aboutForm.linkage_equip_id) {
                        change.equip_id = this.onDevices[i].label;
                        break;
                    }
                }
                if (this.onSlaves[0]) {
                    for (let i = 0, len = this.onSlaves.length; i < len; i++) {
                        if (this.onSlaves[i].value == this.aboutForm.linkage_slave_id) {
                            change.slave_id = this.onSlaves[i].label;
                            break;
                        }
                    }

                }
                for (let i = 0, len = this.onSensors.length; i < len; i++) {
                    if (this.onSensors[i].value == this.aboutForm.linkage_sensor_id) {
                        change.sensor_name = this.onSensors[i].label;
                        break;
                    }
                }
                this.tableData.push(change);
                this.closeAddDialog();

            },

            async linkEdit(row, index) {
                var that = this;
                this.editIndex = index;
                //console.log(this.linkData);
                //console.log(index);
                this.editFormVisible = true;
                //var linkForm=Object.assign({},row);
                this.editForm.type = row.type;
                this.editForm.linkage_value = row.alarm_value;
                this.editForm.linkage_recover_value = row.recover_value;
                for (let i = 0, len = this.onDevices.length; i < len; i++) {
                    if (this.onDevices[i].label == row.equip_id) {
                        this.editForm.linkage_equip_id = this.onDevices[i].value;
                        if (row.type == 1) {
                            await this.getLinkSlave(this.onDevices[i].value);
                            setTimeout(function() {
                                for (let n = 0, nlen = that.linkSlaves.length; n < nlen; n++) {
                                    if (that.linkSlaves[n].label == row.slave_id) {
                                        that.editForm.linkage_slave_id = that.linkSlaves[n].value;
                                        that.getLinkSensor(that.linkSlaves[n].value);
                                        setTimeout(function() {
                                            for (let m = 0, mlen = that.linkSensors.length; m < mlen; m++) {
                                                if (that.linkSensors[m].label == row.sensor_name) {
                                                    that.editForm.linkage_sensor_id = that.linkSensors[m].value;
                                                    break;
                                                }
                                            }
                                        }, 1000)
                                        break;
                                    }
                                }
                            },500);
                        } else {
                            await this.getLinkSensor(this.onDevices[i].value);
                            setTimeout(function() {
                                for (let m = 0, mlen = that.linkSensors.length; m < mlen; m++) {
                                    if (that.linkSensors[m].label == row.sensor_name) {
                                        that.editForm.linkage_sensor_id = that.linkSensors[m].value;
                                        break;
                                    }
                                }
                            }, 500)

                        }
                        break;
                    }
                }
            },

            sureEdit() {
                var that = this;
                if (!this.editForm.linkage_value && (!this.editForm.linkage_recover_value)) {
                    this.$message({
                        message: '触发下发值和恢复下发值不能同时为空!',
                        type: 'error',
                        center: true
                    });
                    return false;
                }
                var about = Object.assign({}, that.editForm);
                delete about.type;
                this.linkData[this.editIndex] = about;
                //console.log(this.linkData);
                this.tableData[that.editIndex].alarm_value = this.editForm.linkage_value;
                this.tableData[that.editIndex].recover_value = this.editForm.linkage_recover_value;
                for (let i = 0, len = this.onDevices.length; i < len; i++) {
                    if (this.onDevices[i].value == this.editForm.linkage_equip_id) {
                        this.tableData[that.editIndex].equip_id = this.onDevices[i].label;
                        break;
                    }
                }
                if (this.linkSlaves[0]) {
                    for (let i = 0, len = this.linkSlaves.length; i < len; i++) {
                        if (this.linkSlaves[i].value == this.editForm.linkage_slave_id) {
                            this.tableData[that.editIndex].slave_id = this.linkSlaves[i].label;
                            break;
                        }
                    }
                }
                for (let i = 0, len = this.linkSensors.length; i < len; i++) {
                    if (this.linkSensors[i].value == this.editForm.linkage_sensor_id) {
                        this.tableData[that.editIndex].sensor_name = this.linkSensors[i].label;
                        break;
                    }
                }
                this.editFormVisible = false;
            },

            cancelEdit() {
                this.editFormVisible = false;
                this.editForm = {
                    linkage_equip_id: '',
                    linkage_slave_id: '',
                    linkage_sensor_id: '',
                    linkage_value: 1,
                    linkage_recover_value: 0,
                };
            },

            async changeEditSlave(slave) {
                var that = this;
                await this.getLinkSensor(slave);
                setTimeout(function() {
                    that.editForm.linkage_sensor_id = that.linkSensors[0].value;
                },500)
            },

            async changeEditDevice(device) {
                var that = this;
                for (let i = 0, len = this.onDevices.length; i < len; i++) {
                    if (this.onDevices[i].value == this.editForm.linkage_equip_id) {
                        this.editForm.type = this.onDevices[i].type;
                        if (this.editForm.type == 1) {
                           await this.getLinkSlave(device);
                            setTimeout(function() {
                                that.editForm.linkage_slave_id = that.linkSlaves[0].value;
                                that.getLinkSensor(that.linkSlaves[0].value);
                                setTimeout(function() {
                                    that.editForm.linkage_sensor_id = that.linkSensors[0].value;
                                }, 1000)
                            }, 500);
                            break;
                        } else {
                           await this.getLinkSensor(device);
                            setTimeout(function() {
                                that.editForm.linkage_sensor_id = that.linkSensors[0].value;
                            },500);
                            break;
                        }
                    }
                }

            },

            closeAddDialog() {
                this.addFormVisible = false;
                this.aboutForm.linkage_equip_id = this.onDevices[0].value;
                this.changeOnDevice(this.onDevices[0].value);
                this.aboutForm.linkage_value = 1;
                this.aboutForm.linkage_recover_value = 0;
            },

            getLinkSlave(id) {
                var that = this;
                this.linkSlaves = [];
                this.$http({
                    method: 'post',
                    url: 'api/slave/all',
                    data: {
                        "equip_id": id

                    }
                }).then(res => {
                    if (res && res.data.result == "success") {
                        for (let i = 0; i < res.data.data.length; i++) {
                            let onslave = {
                                value: res.data.data[i].id,
                                label: res.data.data[i].slave_name
                            }
                            that.linkSlaves.push(onslave)
                        }
                    }
                    // console.log(that.pointdata[0].icon)
                }).catch(error => {
                    console.log(error);

                });
            },

            getLinkSensor(id) {
                var that = this;
                this.linkSensors = [];
                this.$http({
                    method: 'post',
                    url: 'api/sensor/all',
                    data: this.editForm.type == 1?{
                        "slave_id": id

                    } : {
                        "equip_id": id

                    }
                }).then(res => {
                    if (res && res.data.result == "success") {
                        for (let i = 0, len = res.data.data.length; i < len; i++) {
                            if (res.data.data[i].read_write == 2) {
                                let onSensor = {
                                    value: res.data.data[i].id,
                                    label: res.data.data[i].data_key + ' (' + res.data.data[i].sensor_name + ')'
                                }
                                this.linkSensors.push(onSensor);
                            }
                        }
                    }

                }).catch(error => {
                    console.log(error)
                });

            },

            sureDelete(row) {
                var index = this.tableData.indexOf(row);
                //var arr = [];
                if (index != -1) {
                    this.tableData.splice(index, 1);
                    this.linkData.splice(index, 1);
                }
                //console.log(this.linkData);
            },

			switchInfo(){
				this.dialogInfoVisible=true;
			},
			changeCondition(cond){
				if(cond==5){
					this.form.sync_alarm=0;
					this.form.is_linkage=0;
					this.form.is_send=0;
					this.form.recover_alarm=0;
					this.form.is_forward=this.form.equip_id!=0?1:0;
				}else if(this.form.trigger_type==2 && cond!=5){
					this.form.is_forward=0;
				}
			},

			async addForward(){
				var that=this;
				this.addForwardVisible = true;
				this.drawerShow = false;
				//this.isCopy = false;
				this.sensorString = '';
				//不能自己给自己转发，所以要排除掉
				this.forwardDevices = await this.onDevices.filter(function(item,index,arr){
					return item.value!=that.form.equip_id;
				});
				this.addForwardForm.forward_equip_id = this.forwardDevices[0].value;
				this.addForwardForm.forward_equip_code = this.forwardDevices[0].equipCode;
				this.addForwardForm.forward_equip_name = this.forwardDevices[0].label;
				this.addForwardForm.equip_type = this.forwardDevices[0].type;   //1是网关，2是直连
				this.addForwardForm.forward_template=[{
						slave_id:'',
						slave_no: '',
						sensors:[],
						//sensor_id:'',
						data_key:'',
						formula: ''
					}];
				if(this.addForwardForm.equip_type==1){
					this.getForwardSlave(this.forwardDevices[0].value,0,'add');
				} else {
					this.getForwardSensor(this.forwardDevices[0].value,0,'add');
				}
				this.getSlaveSensorTree(this.form.equip_id);
				setTimeout(function(){
				    that.drawerShow = true;
				},500)
			},

			varStringHelp(){
				this.drawerShow=!this.drawerShow;
			},

			//获取设备的从机和变量树
			getSlaveSensorTree(id){
				var that = this;
				this.sensorTree = [];
				this.$http({
				    method: 'post',
				    url: 'api/equip/sensorTree',   //该接口已去掉虚拟变量
				    data: {
				        "id": id,
					}
				}).then(res => {
				    if (res && res.data.result == "success") {
				       if(res.data.data.length){
						   //console.log(res.data.data);
						   this.sensorTree=Array.from(res.data.data);
						   this.$forceUpdate();
					   }
				    }
				}).catch(error => {
				    console.log(error);
				});
			},

			// filterNode(value, data) {
			// 	if (!value) return true;
			// 	return data.label.indexOf(value) !== -1;
			// },

			// getSensorString(data,node,ele){
			// 	//console.log(data);
			// 	if(!data.data_key){
			// 		return
			// 	}
			// 	this.sibling=ele.$el.parentNode.children;
			// 	// for(let i=0;i<this.sibling.length;i++){
			// 	// 	  this.sibling[i].style.color="#333";
			// 	// }
			// 	ele.$el.style.color = "#409eff";
			// 	let str='$devices["'+data.slave_no+'"]["variables"]["'+data.data_key+'"]'+'  ';
			// 	this.sensorString=this.sensorString + str;
			// },

		 //    reSetString(){
			// 	this.sensorString="";
			// 	for(let i=0;i<this.sibling.length;i++){
			// 		this.sibling[i].style.color="#333";
			// 	}
			// },

			// async copyString() {
			// 	var that=this;
			// 	await this.$refs.sensorString.select();
			// 	document.execCommand('copy');
			// 	this.isCopy=true;
			// 	setTimeout(function(){
			// 		that.isCopy=false;
			// 	},5000)
			// 	console.log('复制成功');
			// },

			//丛子组件中获取变量地址字符串
			getSensorString(str){
				this.sensorString=str;
			},

			fillString(item){
				if(this.sensorString){
				   item.formula=this.sensorString.trim();
				}
			},

			changeForwardEquip(id,oper){
				if(oper=='add'){
					if(this.addForwardForm.forward_template.length>1){
						this.addForwardForm.forward_template=[
							{
								slave_id:0,
								sensors:[],
								slave_no: "",
								//sensor_id:0,
								data_key: "",
								formula:""
							}
						];
					}else{
						let formula=this.addForwardForm.forward_template[0].formula
					    this.addForwardForm.forward_template=[
					    	{
					    		slave_id:0,
					    		sensors:[],
					    		slave_no: "",
					    		//sensor_id:0,
					    		data_key: "",
					    		formula:formula
					    	}
					    ];
					}
				}else{
					if(this.editForwardForm.forward_template.length>1){
						this.editForwardForm.forward_template=[
							{
								slave_id:0,
								sensors:[],
								slave_no: "",
								//sensor_id:0,
								data_key: "",
								formula:''
								//formula:formula
							}
						];
					}else{
						let formula=this.editForwardForm.forward_template[0].formula;
						this.editForwardForm.forward_template=[
							{
								slave_id:0,
								sensors:[],
								slave_no: "",
								//sensor_id:0,
								data_key: "",
								formula:'',
								formula:formula
							}
						];
					}
				}


				for(let i=0,len=this.forwardDevices.length;i<len;i++){
					if(this.forwardDevices[i].value==id){
						if(oper=='add'){
							this.addForwardForm.equip_type=this.forwardDevices[i].type;
							this.addForwardForm.forward_equip_code = this.forwardDevices[i].equipCode;
							this.addForwardForm.forward_equip_name = this.forwardDevices[i].label;
							if(this.addForwardForm.equip_type==1){
								this.getForwardSlave(id,0,'add');
							}else{
								this.getForwardSensor(id,0,'add');
							}
						}else{
							this.editForwardForm.equip_type=this.forwardDevices[i].type;
							this.editForwardForm.forward_equip_code = this.forwardDevices[i].equipCode;
							this.editForwardForm.forward_equip_name = this.forwardDevices[i].label;
							if(this.editForwardForm.equip_type==1){
								this.getForwardSlave(id,0,'edit');
							}else{
								this.getForwardSensor(id,0,'edit');
							}
						}
						break;
					}
				}
			},

			getForwardSlave(id,index,oper){
				var that = this;
				this.forwardSlaves = [];
				this.$http({
				    method: 'post',
				    url: 'api/slave/all',
				    data: {
				        "equip_id": id
			        }
				}).then(res => {
				    if (res && res.data.result == "success") {
				        for (let i = 0; i < res.data.data.length; i++) {
				            let slave = {
				                value: res.data.data[i].id,
								slaveNo: res.data.data[i].slave_no,
				                label: res.data.data[i].slave_name
				            }
				            this.forwardSlaves.push(slave);
				        }
						if(oper=='add'){
							for(let i=0,len=this.addForwardForm.forward_template.length;i<len;i++){
								 this.getForwardSensor(this.forwardSlaves[0].value,i,'add');
							}
						}else{
							if(this.editForwardForm.forward_template[index].slave_id){
								this.getForwardSensor(this.editForwardForm.forward_template[index].slave_id,index,'edit');
							}else{
							    this.getForwardSensor(this.forwardSlaves[0].value,index,'edit');
							}
						}
				    }
				}).catch(error => {
				    console.log(error);
			    });
			},

			changeForwardSlave(id,index,oper){
				this.getForwardSensor(id,index,oper);
			},

			getForwardSensor(id,index,oper){
				var that = this;
				var sensors = [];
				if(oper=="add"){
					this.addForwardForm.forward_template[index].sensors=[];
				}else if(oper=="edit"){
				    this.editForwardForm.forward_template[index].sensors=[];
				}
				this.$http({
				    method: 'post',
				    url: 'api/sensor/all',
				    data: (oper == 'add' && this.addForwardForm.equip_type == 1) || (oper == 'edit' && this.editForwardForm.equip_type == 1)?{
				        "slave_id": id

				    } : {
				        "equip_id": id

				    }
				}).then(res => {
				    if (res && res.data.result == "success") {
				        for (let i = 0, len = res.data.data.length; i < len; i++) {
							let sensor = {
								//value: res.data.data[i].id,
								value:res.data.data[i].data_key,
								//label: res.data.data[i].data_key + ' (' + res.data.data[i].sensor_name + ')'
								label:res.data.data[i].sensor_name
							}
							sensors.push(sensor);
				        }
						if(res.data.data.length){
							if(oper=='add'){
								this.addForwardForm.forward_template[index].sensors=Array.from(sensors);
								this.addForwardForm.forward_template[index].data_key = sensors[0].value;
								if(this.addForwardForm.equip_type == 1){
									this.addForwardForm.forward_template[index].slave_id=id;
									for(let i=0,len=this.forwardSlaves.length;i<len;i++){
										if(id==this.forwardSlaves[i].value){
											this.addForwardForm.forward_template[index].slave_no=this.forwardSlaves[i].slaveNo;
											break;
										}
									}
								}
							}else{
								this.editForwardForm.forward_template[index].sensors=Array.from(sensors);
								if(!this.editForwardForm.forward_template[index].data_key){
									this.editForwardForm.forward_template[index].data_key = sensors[0].value;
								}
								if(this.editForwardForm.equip_type == 1){
									this.editForwardForm.forward_template[index].slave_id=id;
									for(let i=0,len=this.forwardSlaves.length;i<len;i++){
										if(id==this.forwardSlaves[i].value){
											this.editForwardForm.forward_template[index].slave_no=this.forwardSlaves[i].slaveNo;
											break;
										}
									}
								}
							}
						}else{
							this.editForwardForm.forward_template[index].data_key ="";
							this.addForwardForm.forward_template[index].data_key = "";
						}
				    }
				}).catch(error => {
				    console.log(error)
				});
			},

			sureAddForward(){
				var that=this;
				var hasEmpty=this.addForwardForm.forward_template.some(function(item,index,arr){
					return item.formula=='';
				});
				if(hasEmpty){
					this.$message({
						message: '计算公式不能为空!',
						type: 'error',
						center: true
				    })
					return
				}
				var newForward=Object.assign({},that.addForwardForm);
				this.form.forwards.push(newForward);
			    this.addForwardVisible = false;

			},

			addDomain(oper) {
			    var that = this;
				var len=1;
				if(oper=="add"){
					len=this.addForwardForm.forward_template.length;
					this.addForwardForm.forward_template.push({
						slave_id:this.addForwardForm.equip_type==1?that.forwardSlaves[0].value:'',
						slave_no: this.addForwardForm.equip_type==1?that.forwardSlaves[0].slaveNo:'',
						sensors:[],
						//sensor_id:'',
						data_key:'',
						formula: ''
					});

					if(this.addForwardForm.equip_type==1){
						//console.log(this.addForwardForm.forward_template.length);
						this.changeForwardSlave(this.forwardSlaves[0].value,len,'add');
					}else{
						//console.log(this.addForwardForm.forward_template.length);
						this.getForwardSensor(this.addForwardForm.forward_equip_id,len,'add');
					}
				}else if(oper=='edit'){
					len=this.editForwardForm.forward_template.length;
					this.editForwardForm.forward_template.push({
						slave_id:this.editForwardForm.equip_type==1?that.forwardSlaves[0].value:'',
						slave_no: this.editForwardForm.equip_type==1?that.forwardSlaves[0].slaveNo:'',
						sensors:[],
						//sensor_id:'',
						data_key:'',
						formula: ''
					});

					if(this.editForwardForm.equip_type==1){
						//console.log(this.addForwardForm.forward_template.length);
						this.changeForwardSlave(this.forwardSlaves[0].value,len,'edit');
					}else{
						//console.log(this.addForwardForm.forward_template.length);
						this.getForwardSensor(this.editForwardForm.forward_equip_id,len,'edit');
					}
					this.editForwardForm.forward_template[len].data_key = this.editForwardForm.forward_template[len].sensors[0].value;
				}
			},

			removeDomain(item,oper){
				if(oper=='add'){
					var index = this.addForwardForm.forward_template.indexOf(item)
					if (index !== -1) {
						this.addForwardForm.forward_template.splice(index, 1)
					}
				}else{
					var index = this.editForwardForm.forward_template.indexOf(item)
					if (index !== -1) {
						this.editForwardForm.forward_template.splice(index, 1)
					}
				}
			},

			showForwardInfo(row,index){
				this.forwardTemplateInfo=Object.assign({},row);
				this.forwardTemplateVisible = true;
			},

			async forwardEdit(row,index){
				var that=this;
				//不能自己给自己转发，所以要排除掉
				this.forwardDevices = await this.onDevices.filter(function(item,index,arr){
					return item.value!=that.form.equip_id;
				});
				this.drawerShow = false;
				this.sensorString = "";
				this.editForwardIndex=index;

				this.changeForwardEquip(row.forward_equip_id,'edit');
				setTimeout(function(){
					that.drawerShow = true;
				    that.editForwardForm = Object.assign({},row);
					that.editForwardForm.forward_template = Array.from(row.forward_template);
				},500)
				this.editForwardVisible = true;
			},

			sureEditForward(){
				var that=this;
				var hasEmpty=this.editForwardForm.forward_template.some(function(item,index,arr){
					return item.formula=='';
				});
				if(hasEmpty){
					this.$message({
						message: '计算公式不能为空!',
						type: 'error',
						center: true
				    })
					return
				}

				this.form.forwards[that.editForwardIndex]=Object.assign({},that.editForwardForm);
				this.editForwardVisible = false;
				// for(let i=0,len=this.forwardDevices.length;i<len;i++){
				// 	if(this.editForwardForm.forward_equip_id==this.forwardDevices[i].value){
				// 		this.editForwardForm.forward_equip_name=this.forwardDevices[i].label;
				// 		this.form.forwards[this.editForwardIndex]=this.editForwardForm;
				// 		this.editForwardVisible = false;
				// 		break;
				// 	}
				// }
			},

			sureDeleteForward(row){
				var index = this.form.forwards.indexOf(row);
				//var arr = [];
				if (index != -1) {
				    this.form.forwards.splice(index, 1);
				}
			},

			cancelAddForward(){
				this.addForwardVisible=false;
			},

			cancelEditForward(){
				this.editForwardVisible = false;
			},

			forwardInfo(){
				this.forwardInfoVisible = true;
			},

			//点击输入框，在光标处插入内容、
			//仅限于原生文本或文本域输入框，这里不适合使用
			// insertTxtAndSetcursor(){
			// 	console.log("a");
			// 	//let element = document.getElementById("formula"); // 获取到指定标签
			// 	let startPos = element.selectionStart; // 获取光标开始的位置
			// 	let endPos = element.selectionEnd; // 获取光标结束的位置
			// 	if (startPos === undefined || endPos === undefined) return; // 如果没有光标位置 不操作

			// 	let oldTxt = element.value;
			// 	 console.log(oldTxt);// 获取输入框的文本内容
			// 	let result = oldTxt.substring(0, startPos) + '这里是插入的内容' + oldTxt.substring(endPos); // 将文本插入
			// 	//this.editForm.formula = result; // 将拼接好的文本设置为输入框的值
			// 	element.focus(); // 重新聚焦输入框
			// 	element.selectionStart = startPos + insertTxt.length; // 设置光标开始的位置
			// 	element.selectionEnd = startPos + insertTxt.length; // 设置光标结束的位置
			// },

			addGoods() {
                var that = this;
				this.form.linkages=Array.from(this.linkData);
                if (typeof(this.form.data_key) == "object") { //判断是否为数组对象
                    this.form.data_key = this.form.data_key.join(',');
                };
				if(this.form.forwards && this.form.forwards.length){
					for(let i=0,len=this.form.forwards.length;i<len;i++){
						for(let n=0,len2=this.form.forwards[i].forward_template.length;n<len2;n++){
							delete this.form.forwards[i].forward_template[n].sensors;
						}
					}
				}
                if (this.form.trigger_type == 2) {
                    this.form.recover_alarm = "";
                };
                this.$http({
                    method: 'post',
                    url: 'api/trigger/add',
                    data: this.form
		        }).then(res => {
                    if (res && res.data.result == "success") {
                        this.$message({
                            message: '恭喜,添加触发器成功!',
                            type: 'success',
                            center: true
                        });
                        this.$router.go(-1);
                    }

                }).catch(error => {
                    console.log(error);
                });
            }

        }
    }
</script>

<style scoped>
    @media all and (max-width:2560px) {
        .demo {
            margin-left: -1.2rem!important;
			margin-top:0.2rem!important;
        }
        .tip2 {
            width: 60%!important;
        }
    }

    @media all and (max-width:1920px) {
        .demo {
            margin-left: -1.2rem!important;
		}
        .tip2 {
            width:45%!important;
        }
    }

    @media all and (max-width:1600px) {
        .demo {
            margin-left: -1.4rem!important;
        }
        .add {
            bottom: 0.1rem!important;
        }
        .tip2 {
            width: 50%!important;
        }
    }

    @media all and (max-width:1440px) {
        .demo {
            margin-left: -1.5rem!important;
        }
        .tip2 {
            width: 48%!important;
            right: 0.5rem!important;
        }
    }

    @media all and (max-width:1366px) {
        .demo {
            margin-left: -1.9rem!important;
            margin-top: 0.3rem!important;
        }
        .tip2 {
            width: 43%!important;
            right: 0.5rem!important;
        }
    }

    #addform {
        /* float: left; */
        text-align: left;
        margin-top: 0.2rem;
        width: 99.4%;
        /* height:14rem; */
        line-height: 0.7rem;
        /* border:1px solid red; */
    }

    #linkForm {
        /* position:relative; */
        line-height: 0.6rem;
        float: left;
        /* margin-top: 0.2rem; */
        /* margin-left:1rem; */
        text-align: left;
        width: 99.4%;
        margin-top: -0.3rem;
        padding-top: 0.2rem;
        z-index: 10;
        border-top: 1px solid #ddd;
    }

    .inpu {
        display: inline-block;
        vertical-align: middle;
        width: 4.5rem;
		font-size:0.3rem;
        /* font-size: 14px; */
    }

    .tip {
        color: #FF5722;
        width: 4.5rem;
        font-size: 0.25rem;
        margin-top: 0.2rem;
        height: 0.8rem;
        line-height: 0.4rem;
        margin-bottom: 0rem;
    }

    .line {
        width: 100%;
        position: relative;
        height: auto;
        margin-top: 0.1rem;
        padding-top: 0.3rem;
        /* margin-top:-0.4rem;
		margin-bottom:0.5rem; */
        border: 1px dashed #ddd;
    }

    .leftName {
        position: absolute;
        top: -1px;
        left: -1px;
        width: auto;
        padding: 0 0.1rem;
        display: inline-block;
        height: 0.6rem;
        line-height: 0.6rem;
        font-size: 0.3rem;
        background-color: #222831;
        /*#22567b*/
        color: #fff;
    }

    .tip2 {
        width:50%;
        position: absolute;
        top: -2px;
        right: 0.2rem;
        height: 1.4rem;
        line-height: 0.35rem;
		border:1px solid red;
    }

    .add {
       /* position: absolute;
        left: 80px;
        bottom: 0.2rem; */
        font-size: 0.3rem;
        color: #409Eff;
		/* border:none; */
        border: 1px solid #409Eff;
    }

    #notice {
        position: absolute;
        right:20px;
        top:30px;
		z-index:3;
        height: 0.6rem;
        line-height: 0.6rem;
        width: auto;
        font-size: 0.3rem;
        color: red;
    }

    #addform /deep/ .el-form-item {
        margin-bottom: 10px;
    }

    .typeBox{
		display: inline-block;
		width:0.5rem;
		height:0.5rem!important;
		line-height:0.5rem;
		text-align:center;
		transform: translateY(5px);
		margin:0;
		padding-top:1px;
		cursor:pointer;
		border-radius: 4px;

	}

	.isChoose{
		border:1px solid #409eff;
		color:#fff;
		background-color:#409eff;
	}
	.onChoose{
		border:1px solid #ccc;
		color:#fff;
		background-color:#fff;
	}

	.inpu /deep/ .el-input__inner{
		padding-left:4px;
		padding-right:4px;
	}

	.inpu /deep/ .el-textarea__inner{
		padding-left:4px;
		padding-right:4px;
	}

	.sortIcon {
		display:inline-block;
		vertical-align: middle;
		margin-left:0.1rem;
	    width: 0.3rem!important;
	    height: 0.3rem;
	    font-size: 0.25rem;
	    line-height: 0.3rem;
	    text-align: center;
	    color: #999;
	    background-color: #ccc;
	}

	.infocontent{
		text-align: left;
		line-height: 0.42rem;
	}

	h4{
		font-size:0.3rem;
		color: #111;
		font-weight:700;
		margin-top:0.2rem;
	}

	.sensorHead {
	    height: 0.6rem;
	    line-height: 0.6rem;
		text-align:left;
	    /* border:1px solid red; */
	    width: 99.6%;
	    border-bottom: 1px solid #111;
	    margin: 0 0 0.2rem 0;
	    padding: 0;
	    padding-left: 0.2rem;
	}

	.sensorHead  .inpu{
		text-align: center;
		font-size:0.3rem;
		margin-right:0.1rem;
	}

	.oneDomain {
	    margin-left: 0.2rem;
	    height: 0.6rem;
	    line-height: 0.6rem;
	    width: 98%;
	    white-space: nowrap;
	}

	.oneDomain  .inpu{
		margin-right:0.1rem
	}

	.datadial>>>.el-dialog__header {
		position: relative;
		z-index: 99;
		width: 85%;
		height: 0.8rem;
		background-color: transparent !important;
	}

	.datadial>>>.el-dialog__body {
		padding-bottom: 10px;
		padding-top: 10px;
	}
</style>
